package cn.org.redis.study;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Transaction;

import java.util.List;
import java.util.Set;

/**
 * @ClassName JedisStudy
 * @Author swj
 * @Date 2020/9/23 19:38
 */
public class JedisStudy {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.44.128",6379);

        //jedis.set("jedis","hello");
        //String key = "jedis";
        System.out.println(jedis.get("jedis"));
        Set set = jedis.keys("*");
        System.out.println(set.toString());
        //jedis.lpush("list1","hello","world");
        List list = jedis.lrange("list1",0,-1);
        System.out.println(list.toString());
        //alpha---根据首字母来排序
        SortingParams params = new SortingParams();
        System.out.println(jedis.sort("list1",params.alpha()));

        //管道的使用，统一发送到服务端

        String key = "pipe1";
        jedis.set(key,"10");
        Pipeline pipeline =  jedis.pipelined();
        for (int i = 0; i < 10; i++) {
            pipeline.incr(key);
        }
        pipeline.sync();
        System.out.println(jedis.get(key));

        //事务测试类
        jedis.set("edward","8888");
        jedis.set("yuqing","6666");
        Transaction transaction = jedis.multi();
        transaction.incrBy("edward",2222);
        transaction.decrBy("yuqing",2222);

        transaction.exec();
        System.out.println(jedis.mget("edward","yuqing").toString());


        jedis.close();
    }
}
